<template>
  <div>
    <van-nav-bar
      fixed
      placeholder
      title="我的求购"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />
    <van-cell-group inset v-for="item in needsList" :key="item.needsId">
      <van-cell
        :title="item.needsXm"
        :label="item.needsContent"
        :value="$moment(item.addTime).format('MM-DD')"
      />
      <van-row type="flex" justify="end">
        <van-col span="4">
          <van-button
            type="primary"
            size="small"
            @click="deleteNl(item.needsId)"
            >删除</van-button
          >
        </van-col>
      </van-row>
    </van-cell-group>
  </div>
</template>

<script>
import Vue from "vue";
import { Toast } from "vant";

Vue.use(Toast);
export default {
  name: "ToutiaoMuqiu",

  components: {},

  directives: {},

  data() {
    return {
      needsList: [],
    };
  },

  mounted() {},
  created() {
    this.getAllneeds();
  },
  methods: {
    // 获取求购列表
    async getAllneeds() {
      let that = this;
      let data = {
        finish: 0,
        userId: localStorage.getItem("id"),
      };

      const { data: res } = await this.$http.get("/api/needs/list", {
        params: data,
      });
      that.needsList = res.data;
    },
    deleteNl(id) {
      let that = this;
      $.ajax({
        type: "DELETE",
        url: "http://121.4.40.144/api/needs/" + id,
        beforeSend: function (request) {
          request.setRequestHeader("token", localStorage.getItem("token"));
        },
        success: function (res) {
          if (res.code !== 200) {
            Toast("删除失败");
          } else {
            Toast("删除成功");
            that.getAllneeds();
          }
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.van-cell__label {
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  white-space: nowrap;
  width: 240px;
}
</style>